#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 23 13:41:50 2019

@author: gloria

GRAPH A3

"""


# Select computer
computer = 'glgennaro' # work 
# computer = 'gloria' #personal



import pandas as pd
import os 
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter



from matplotlib import rcParams
# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/dta')
df = pd.read_stata('congress_election_complete.dta')

#df = df[(df['party_short']=='D') | (df['party_short']=='R')]


# gender 
df_f = df.groupby(['gender']).mean().reset_index()
df_f = df_f[['gender','pop_dim2']]
df_f.columns = ['gender', 'mean']
df_s = df.groupby(['gender']).std().reset_index()
df_s = df_s[['gender','pop_dim2']]
df_s.columns = ['gender', 'std']
df_t = pd.merge(df_f, df_s, on='gender')

df_t['label'] = ['Female', 'Male']

x = np.array(df_t.index)
y = np.array(df_t['mean'])
e = np.array(df_t['std'])
x_pos = np.arange(len(df_t.index))

fig, ax = plt.subplots()
ax.bar(x_pos, y, yerr=e, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Populism', size=30)
ax.set_xticks(x_pos)
ax.set_xticklabels(df_t['label'], rotation=0,  size=30)
ax.yaxis.grid(True)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.tick_params(axis='y', which='major', labelsize=15);


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('cong_gender.png')

   


# incumbent 

df_f = df.groupby(['incumbent']).mean().reset_index()
df_f = df_f[['incumbent','pop_dim2']]
df_f.columns = ['incumbent', 'mean']
df_s = df.groupby(['incumbent']).std().reset_index()
df_s = df_s[['incumbent','pop_dim2']]
df_s.columns = ['incumbent', 'std']
df_t = pd.merge(df_f, df_s, on='incumbent')

df_t['label'] = ['Non Incumbent', 'Incumbent']

x = np.array(df_t.index)
y = np.array(df_t['mean']) 
e = np.array(df_t['std'])
x_pos = np.arange(len(df_t.index))

fig, ax = plt.subplots()
ax.bar(x_pos, y, yerr=e, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Populism', size=30)
ax.set_xticks(x_pos)
ax.set_xticklabels(df_t['label'], rotation=0,  size=30)
ax.yaxis.grid(True)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.tick_params(axis='y', which='major', labelsize=15);


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('cong_incumbent.png')

   


# outsider

df_f = df.groupby(['outsider']).mean().reset_index()
df_f = df_f[['outsider','pop_dim2']]
df_f.columns = ['outsider', 'mean']
df_s = df.groupby(['outsider']).std().reset_index()
df_s = df_s[['outsider','pop_dim2']]
df_s.columns = ['outsider', 'std']
df_t = pd.merge(df_f, df_s, on='outsider')

df_t['label'] = ['Insider', 'Outsider']

x = np.array(df_t.index)
y = np.array(df_t['mean']) 
e = np.array(df_t['std'])
x_pos = np.arange(len(df_t.index))

fig, ax = plt.subplots()
ax.bar(x_pos, y, yerr=e, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Populism', size=30)
ax.set_xticks(x_pos)
ax.set_xticklabels(df_t['label'], rotation=0,  size=30)
ax.yaxis.grid(True)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.tick_params(axis='y', which='major', labelsize=15);


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('cong_outsider.png')



# party
df_f = df.groupby(['party_short']).mean().reset_index()
df_f = df_f[['party_short','pop_dim2']]
df_f.columns = ['party_short', 'mean']
df_s = df.groupby(['party_short']).std().reset_index()
df_s = df_s[['party_short','pop_dim2']]
df_s.columns = ['party_short', 'std']
df_t = pd.merge(df_f, df_s, on='party_short')

df_t['label'] = ['Democrats', 'Republicans', 'Others']

x = np.array(df_t.index)
y = np.array(df_t['mean']) # Effectively y = x**2
e = np.array(df_t['std'])
x_pos = np.arange(len(df_t.index))

fig, ax = plt.subplots()
ax.bar(x_pos, y, yerr=e, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Populism', size=30)
ax.set_xticks(x_pos)
ax.set_xticklabels(df_t['label'], rotation=0,  size=30)
ax.yaxis.grid(True)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.tick_params(axis='y', which='major', labelsize=15);


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('cong_democrat.png')




# competitive
df_f = df.groupby(['competitivedistricts']).mean().reset_index()
df_f = df_f[['competitivedistricts','pop_dim2']]
df_f.columns = ['competitivedistricts', 'mean']
df_s = df.groupby(['competitivedistricts']).std().reset_index()
df_s = df_s[['competitivedistricts','pop_dim2']]
df_s.columns = ['competitivedistricts', 'std']
df_t = pd.merge(df_f, df_s, on='competitivedistricts')

df_t['label'] = ['Non Competitive', 'Competitive']


x = np.array(df_t.index)
y = np.array(df_t['mean']) # Effectively y = x**2
e = np.array(df_t['std'])
x_pos = np.arange(len(df_t.index))

fig, ax = plt.subplots()
ax.bar(x_pos, y, yerr=e, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Populism', size=30)
ax.set_xticks(x_pos)
ax.set_xticklabels(df_t['label'], rotation=0,  size=30)
ax.yaxis.grid(True)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.tick_params(axis='y', which='major', labelsize=15);


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('cong_comp.png')



# education
df4 = df[['education', 'pop_dim2']]
df4['educ']=df4['education']
for i in range(len(df4['educ'])):
    if df4['educ'][i]=='MBA':
        df4['educ'][i]='college degree'
    elif df4['educ'][i]=='Phd':
        df4['educ'][i]='college degree'
    else:
        continue


df_f = df4.groupby(['educ']).mean().reset_index()
df_f = df_f[['educ','pop_dim2']]
df_f.columns = ['educ', 'mean']
df_s = df4.groupby(['educ']).std().reset_index()
df_s = df_s[['educ','pop_dim2']]
df_s.columns = ['educ', 'std']
df_t = pd.merge(df_f, df_s, on='educ')
df_t = df_t[1:]

df_t['label'] = ['Secondary', 'Tertiary +']

x = np.array(df_t.index)
y = np.array(df_t['mean']) 
e = np.array(df_t['std'])
x_pos = np.arange(len(df_t.index))

fig, ax = plt.subplots()
ax.bar(x_pos, y, yerr=e, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Populism', size=30)
ax.set_xticks(x_pos)
ax.set_xticklabels(df_t['label'], rotation=0,  size=30)
ax.yaxis.grid(True)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.tick_params(axis='y', which='major', labelsize=15);


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/do/Replication package 2020 v3_2measures/Replication package 2020 v3/Graphs')
plt.savefig('cong_educ.png')

